package com.bdqn.boot.yuanshen.controller;

import com.bdqn.boot.yuanshen.bean.Interactions;
import com.bdqn.boot.yuanshen.service.InteractionsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 周宏锦
 * @Description TODO
 * @Date 2025/3/21 11:24
 * @Version 1.0
 */
@Controller
public class InteractionsController {
    @Autowired
    private InteractionsService interactionsService;
    @PutMapping("/interactions")
    public Map<String,Object> save(@RequestBody Interactions interactions){
        Map<String,Object> map = new HashMap<>();
        try {
            if (interactions.getId()!=null){
                int result = interactionsService.updateInteractions(interactions);
                if (result > 0) {
                    map.put("success", true);
                }
            }else {
                int result = interactionsService.addInteractions(interactions);
                if (result > 0) {
                    map.put("success", true);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg",e.getMessage());
    }
        return map;
    }
    @DeleteMapping("/interactions")
    public Map<String,Object> del(@PathVariable("id")Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            int result = interactionsService.delInteractions(id);
            if (result>0){
                map.put("success",true);
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/interactions/page")
    public PageInfo<Interactions> getNotificationsList(@RequestBody Map<String,Interactions> map,
                                                        @RequestParam(defaultValue = "1") int pageNum,
                                                        @RequestParam(defaultValue = "10") int pageSize){
        return interactionsService.getInteractionsList(map,pageNum,pageSize);
    }
}
